<template>
  <!-- 服务管理 -->
  <div class="innerPageTable-box-OneTwo value-set-list-wrap innerPageTable-out-flex">
    <!-- 搜索区域 -->
    <SelfAdaption>
      <Form
        ref="searchForm"
        :model="searchForm"
        slot="adaption-bar"
        label-position="top"
        class="ivu-form-no-margin-bottom"
        inline
      >
        <Form-item prop="stage" :label="'评分阶段'">
          <Select v-model="searchForm.stage" clearable @on-change="searchValueSet">
            <Option v-for="item in stageList" v-model="item.key" :key="item.key">{{ item.value }}</Option>
          </Select>
        </Form-item>
        <Form-item prop="period" :label="'当前评分周期'">
          <Select v-model="searchForm.period" clearable @on-change="searchValueSet">
            <Option v-for="item in periodList" :value="item" :key="item">{{ item }}</Option>
          </Select>
        </Form-item>

        <!-- 查询\重置按钮 -->
        <Form-item prop="button" class="search-solt-btn">
          <RPButton optType="search" @click="searchValueSet()" />
          <RPButton optType="reset" @click="resetAndSearchValueSet()" />
        </Form-item>
      </Form>
    </SelfAdaption>
    <InnerPageTable ref="valueSetTable" :config="pageTableConfig"> </InnerPageTable>
  </div>
</template>

<script>
import permission from '@/mixin/permission'
import { reportStatus, STATUS_TYPE_APPROVED } from '@/views/formConst'
import { summaryTableColumns } from './columns'
import dataApi from '@/api/formData'
import formCenterApi from '@/api/formCenter'
import { STATUS_TYPE_PUBLISHED } from '@/views/formConst'
import { mapState } from 'vuex'
import summaryDetail from './components/summaryDetail'
export default {
  name: 'summary',
  mixins: [permission],
  props: {
    row: {
      type: Object,
      default: () => {}
    },
    onlyView: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      userCode: sessionStorage.userCode,
      operateForms: [],
      formPermissions: {},
      forms: [],
      formType: '',
      subTypeList: [],
      reportList: reportStatus,
      periodList: [],
      stageList: [
        { key: 1, value: '第1轮评分' },
        { key: 2, value: '第2轮评分' },
        { key: 3, value: '第3轮评分' }
      ],
      operateIds: [],
      searchForm: {
        period: '',
        status: '',
        stage: null
      },
      formIdList: [],
      pageTableConfig: {
        loadDataFn: this.getServiceList,
        columns: summaryTableColumns.bind(this)(),
        autoFirst: false //默认 就是 false, 要配置在这里
      }
    }
  },
  computed: {
    ...mapState(['orgList'])
  },
  mounted() {
    if (this.orgList.length <= 0) {
      this.$store.dispatch('getOrgList')
    }
    // if (this.hasPermission('FORM_CREATE')) {
    //   //制表员（智数部人员默认显示已审批的）
    //   this.searchForm.status = STATUS_TYPE_APPROVED
    // }
    this.init()
  },
  methods: {
    init() {
      //快速跳转
      formCenterApi.menuFindList({ menu: this.row.value, status: STATUS_TYPE_PUBLISHED }).then(res => {
        this.operateForms = res.data.data.filter(item => item.permission.operate)
        this.forms = res.data.data
        this.formPermissions = {}
        this.forms.forEach(element => {
          this.formPermissions[element.id] = element.permission
        })
        this.formIdList = this.forms.map(item => item.id)
        this.subTypeList = [...new Set(this.forms.filter(item => item.subType !== '').map(item => item.subType))]
        this.operateIds = Array.from(new Set(this.forms.map(item => item.createBy)))
        if (this.formIdList.length > 0) {
          this.formType = this.forms[0].type
          this.getPeriodList(this.formIdList)
          this.searchValueSet()
        }
      })
    },
    async getPeriodList(formIds) {
      const res = await formCenterApi.formReportHistoryPeriod(formIds)
      this.periodList = Array.from(new Set(res.data))
    },
    //评委维度
    judgesSummary(row) {
      new this.$sidelipPage(summaryDetail, {
        props: {
          row: row,
          type: 0
        }
      })
    },
    // 指标维度
    indicatorSummary(row) {
      new this.$sidelipPage(summaryDetail, {
        props: {
          row: row,
          type: 1
        }
      })
    },
    getServiceList(tableObj) {
      let { responseProcess, param } = tableObj
      const conditions = []
      Object.keys(this.searchForm).forEach(key => {
        let value = this.searchForm[key]
        if ((value && typeof value !== 'object') || Object.is(value, 0)) {
          conditions.push({
            fieldId: key,
            operator: 'is',
            value: value
          })
        }
      })
      param.condition.filterInfo = { conjunction: 'and', conditions }
      param.condition.formType = this.formType
      param.condition.formIdList = this.formIdList
      dataApi
        .summaryPage({
          ...param,
          formIdList: this.formIdList,
          pageSize: param.commonPageDTO.pageSize,
          currentPage: param.commonPageDTO.currentPage
        })
        .then(res => {
          let { pageSize, total, current, records, pages } = res.data.data
          records.forEach(item => {
            item.showstage = this.getStageShowWord(item.stage)
          })
          responseProcess({
            pageNum: current,
            pageSize: pageSize,
            pages: pages,
            records: records || [],
            total: total
          })
        })
    },
    searchValueSet(isCurrent) {
      if (!isCurrent) {
        this.$refs['valueSetTable'].currentPage = 1
      }
      this.$refs['valueSetTable'].load(this.searchForm)
    },
    getStageShowWord(stage) {
      return `第${stage}轮评分`
    },
    resetAndSearchValueSet() {
      this.$refs['searchForm'].resetFields()
      this.searchValueSet()
    }
  }
}
</script>
